﻿Imports System.Data.SqlClient

Public Class clsCatalogoTanques
    Private mintIdTanque As Integer
    Private mstrNombre As String
    Private mstrUbicacion As String
    Private mdblLongitud As Double
    Private mdblDiametro As Double
    Private mdblAltoGlobo As Double
    Private mdblCapacidad As Double
    Private mdblMedida_Actual As Double
    Private mintIdAlmacen As Integer
    Private mintIdBomba As Integer
    Private mdblLitrosActuales As Double
    Private mintPosicionTanque As Integer
    Private mdatUltimaFecha As Date
    Private mdblCuentaLitros As Double
    Private mdblLtsAproximados As Double
    Private mstrIP_Scanner As String

    Public Property IdTanque As Integer
        Get
            Return mintIdTanque
        End Get
        Set(value As Integer)
            mintIdTanque = value
        End Set
    End Property
    Public Property Nombre As String
        Get
            Return mstrNombre
        End Get
        Set(value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Ubicacion As String
        Get
            Return mstrUbicacion
        End Get
        Set(value As String)
            mstrUbicacion = value
        End Set
    End Property
    Public Property Longitud As Double
        Get
            Return mdblLongitud
        End Get
        Set(value As Double)
            mdblLongitud = value
        End Set
    End Property
    Public Property Diametro As Double
        Get
            Return mdblDiametro
        End Get
        Set(value As Double)
            mdblDiametro = value
        End Set
    End Property
    Public Property AltoGlobo As Double
        Get
            Return mdblAltoGlobo
        End Get
        Set(value As Double)
            mdblAltoGlobo = value
        End Set
    End Property
    Public Property Capacidad As Double
        Get
            Return mdblCapacidad
        End Get
        Set(value As Double)
            mdblCapacidad = value
        End Set
    End Property
    Public Property Medida_Actual As Double
        Get
            Return mdblMedida_Actual
        End Get
        Set(value As Double)
            mdblMedida_Actual = value
        End Set
    End Property
    Public Property IdAlmacen As Integer
        Get
            Return mintIdAlmacen
        End Get
        Set(value As Integer)
            mintIdAlmacen = value
        End Set
    End Property
    Public Property IdBomba As Integer
        Get
            Return mintIdBomba
        End Get
        Set(value As Integer)
            mintIdBomba = value
        End Set
    End Property
    Public Property LitrosActuales As Double
        Get
            Return mdblLitrosActuales
        End Get
        Set(ByVal value As Double)
            mdblLitrosActuales = value
        End Set
    End Property
    Public Property PosicionTanque As Integer
        Get
            Return mintPosicionTanque
        End Get
        Set(ByVal value As Integer)
            mintPosicionTanque = value
        End Set
    End Property
    Public Property UltimaFecha As Date
        Get
            Return mdatUltimaFecha
        End Get
        Set(ByVal value As Date)
            mdatUltimaFecha = value
        End Set
    End Property
    Public Property Cuentalitros As Double
        Get
            Return mdblCuentaLitros
        End Get
        Set(ByVal value As Double)
            mdblCuentaLitros = value
        End Set
    End Property
    Public Property LtsAproximados As Double
        Get
            Return mdblLtsAproximados
        End Get
        Set(ByVal value As Double)
            mdblLtsAproximados = value
        End Set
    End Property
    Public Property IP_Scanner As String
        Get
            Return mstrIP_Scanner
        End Get
        Set(ByVal value As String)
            mstrIP_Scanner = value
        End Set
    End Property
    Public Sub New()
        mintIdTanque = 0
        mstrNombre = ""
        mstrUbicacion = ""
        mdblAltoGlobo = 0
        mdblCapacidad = 0
        mdblDiametro = 0
        mdblLongitud = 0
        mdblMedida_Actual = 0
        mintIdAlmacen = 0
        mintIdBomba = 0
        mdblLitrosActuales = 0
        mintPosicionTanque = 0
        mdatUltimaFecha = Now()
        mdblCuentaLitros = 0
        mdblLtsAproximados = 0
        mstrIP_Scanner = ""
    End Sub
    Public Sub Guardar()
        If IdTanque = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer = BuscarIntDatoCitra("Tanques", "IdTanque", "IdTanque=" & IdTanque)

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()
        End If
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = "SELECT * FROM Tanques WHERE Nombre='" & Nombre & "'"

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            If rdBuscar.Read() Then
                mintIdTanque = rdBuscar("IdTanque")
                mstrNombre = rdBuscar("Nombre").ToString.Trim
                mstrUbicacion = rdBuscar("Ubicacion").ToString.Trim
                mdblAltoGlobo = rdBuscar("AltoGlobo")
                mdblCapacidad = rdBuscar("Capacidad")
                mdblDiametro = rdBuscar("Diametro")
                mdblLongitud = rdBuscar("Longitud")
                mdblMedida_Actual = rdBuscar("Medida_Actual")
                mintIdAlmacen = rdBuscar("IdAlmacen")
                mintIdBomba = rdBuscar("IdBomba")
                mdblLitrosActuales = rdBuscar("LitrosActuales")
                mintPosicionTanque = rdBuscar("PosicionTanque")
                mdatUltimaFecha = rdBuscar("UltimaFecha")
                mdblCuentaLitros = rdBuscar("CuentaLitros")
                mdblLtsAproximados = rdBuscar("LtsAproximados")
                mstrIP_Scanner = rdBuscar("IP_Scanner")
            Else
                mintIdTanque = 0
                mstrNombre = ""
                mstrUbicacion = ""
                mdblAltoGlobo = 0
                mdblCapacidad = 0
                mdblDiametro = 0
                mdblLongitud = 0
                mdblMedida_Actual = 0
                mintIdAlmacen = 0
                mintIdBomba = 0
                mdblLitrosActuales = 0
                mintPosicionTanque = 0
                mdatUltimaFecha = Now()
                mdblCuentaLitros = 0
                mdblLtsAproximados = 0
                mstrIP_Scanner = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Tanques (" & _
                            "IdTanque," & _
                            "Nombre," & _
                            "Ubicacion," & _
                            "Longitud," & _
                            "Diametro," & _
                            "AltoGlobo," & _
                            "Capacidad," & _
                            "Medida_Actual," & _
                            "IdAlmacen," & _
                            "IdBomba, " & _
                            "LitrosActuales," & _
                            "PosicionTanque," & _
                            "UltimaFecha," & _
                            "Cuentalitros," & _
                            "LtsAproximados," & _
                            "IP_Scanner)" & _
                            "VALUES (" & _
                            "@IdTanque," & _
                            "@Nombre," & _
                            "@Ubicacion," & _
                            "@Longitud," & _
                            "@Diametro," & _
                            "@AltoGlobo," & _
                            "@Capacidad," & _
                            "@Medida_Actual," & _
                            "@IdAlmacen," & _
                            "@IdBomba," & _
                            "@LitrosActuales," & _
                            "@PosicionTanque," & _
                            "@UltimaFecha," & _
                            "@CuentaLitros," & _
                            "@LtsAproximados," & _
                            "@IP_Scanner)"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdTanque", mintIdTanque)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Ubicacion", mstrUbicacion)
        Command.Parameters.AddWithValue("Longitud", mdblLongitud)
        Command.Parameters.AddWithValue("Diametro", mdblDiametro)
        Command.Parameters.AddWithValue("AltoGlobo", mdblAltoGlobo)
        Command.Parameters.AddWithValue("Capacidad", mdblCapacidad)
        Command.Parameters.AddWithValue("Medida_Actual", mdblMedida_Actual)
        Command.Parameters.AddWithValue("IdAlmacen", mintIdAlmacen)
        Command.Parameters.AddWithValue("IdBomba", mintIdBomba)
        Command.Parameters.AddWithValue("LitrosActuales", mdblLitrosActuales)
        Command.Parameters.AddWithValue("PosicionTanque", mintPosicionTanque)
        Command.Parameters.AddWithValue("UltimaFecha", mdatUltimaFecha)
        Command.Parameters.AddWithValue("Cuentalitros", mdblCuentaLitros)
        Command.Parameters.AddWithValue("LtsAproximados", mdblLtsAproximados)
        Command.Parameters.AddWithValue("IP_Scanner", mstrIP_Scanner)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el tanque" & mstrNombre, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update Tanques Set " & _
                    " Nombre=@Nombre, " & _
                    " Ubicacion=@Ubicacion, " & _
                    " Longitud=@Longitud, " & _
                    " Diametro=@Diametro, " & _
                    " AltoGlobo=@AltoGlobo, " & _
                    " Capacidad=@Capacidad, " & _
                    " IdAlmacen=@IdAlmacen " & _
                    " where IdTanque=@IdTanque"

        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdTanque", mintIdTanque)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Ubicacion", mstrUbicacion)
        Command.Parameters.AddWithValue("Longitud", mdblLongitud)
        Command.Parameters.AddWithValue("Diametro", mdblDiametro)
        Command.Parameters.AddWithValue("AltoGlobo", mdblAltoGlobo)
        Command.Parameters.AddWithValue("Capacidad", mdblCapacidad)
        Command.Parameters.AddWithValue("Medida_Actual", mdblMedida_Actual)
        Command.Parameters.AddWithValue("IdAlmacen", mintIdAlmacen)
        Command.Parameters.AddWithValue("IdBomba", mintIdBomba)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la bomba " & mstrNombre, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
